﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.UI.Views;
using System.Windows.Input;

namespace ARR.UI.Commands
{
    public abstract class CommandBase : ICommand
    {
        public virtual event EventHandler CanExecuteChanged;

        public string ConfirmationMessage { get; set; }

        public Func<object, string> ConfirmationMessageDelegate = x => null;

        public string ReturnMessage { get; set; }

        public Func<object, string> ReturnMessageDelegate = x => null;

        public virtual bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            string confirmationMessage = this.ConfirmationMessage ?? ConfirmationMessageDelegate(parameter);
            if (confirmationMessage != null)
            {
                /*
                if (MessageBox.Show(confirmationMessage, Labels.ApplicationTitle, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) != MessageBoxResult.Yes)
                    return;
                 */

                Confirm confirmWindow = new Confirm(confirmationMessage);
                bool? confirm = confirmWindow.ShowDialog();
                if (!confirm.GetValueOrDefault())
                    return;
            }

            this.ExecuteInternal(parameter);

            string returnMessage = this.ReturnMessage ?? ReturnMessageDelegate(parameter);
            {
                if (returnMessage != null)
                {
                    //MessageBox.Show(returnMessage, Labels.ApplicationTitle, MessageBoxButton.OK, MessageBoxImage.Information);
                    Alert alertWindow = new Alert(returnMessage);
                    alertWindow.ShowDialog();
                }
            }
        }

        protected abstract void ExecuteInternal(object parameter);
    }
}
